package com.luyuan.crm.user.support;

import java.util.List;

import org.hibernate.criterion.Order;
import org.springframework.security.providers.encoding.PasswordEncoder;

import com.luyuan.crm.user.service.UserMemberService;
import com.ruijie.components.security.model.AppUser;
import com.ruijie.foundation.dao.extend.support.HibernateEntityExtendDao;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaQueryTemplate;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;

public class UserMemberServiceImpl extends HibernateEntityExtendDao<AppUser> implements UserMemberService {

    private PasswordEncoder passwordEncoder;

    @Override
    public AppUser getById(String id) {
        return safeGet(id);
    }

    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public Page pagedQuery(CriteriaTemplate criteria, int pageNo, int pageSize, List<Order> orders) {
        CriteriaQueryTemplate qt = new CriteriaQueryTemplate(getSessionFactory());
        return qt.pagedQuery(criteria, pageNo, pageSize, orders);

    }

    @Override
    public void updatePassword(String loginName, String password) {
        AppUser appUser = findUniqueBy("username", loginName);
        appUser.setPassword(passwordEncoder.encodePassword(password, null));
        update(appUser);
    }
}
